package com.demo.messagebook.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jhello.core.annotations.web.At;
import com.jhello.core.annotations.web.Controller;
import com.jhello.core.model.Model;
import com.jhello.core.modelview.ModelAndView;
import com.jhello.core.pub.HttpMethod;
import com.jhello.core.view.JsonView;

@Controller
public class MessageBookController {
	
	@At("/")
	public ModelAndView index(){
		List<Map<String,Object>> data = prepareListData();
		Model model = new Model();
		model.put("messageList", data);
		return new ModelAndView("list.jsp", model);
	}
	
	@At(value = "/list",method = HttpMethod.POST)
	public ModelAndView list(HttpServletRequest req,HttpServletResponse resp){
		List<Map<String,Object>> data = prepareListData();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", "id11");
		map.put("message", req.getParameter("message"));
		data.add(map);
		Model model = new Model();
		model.put("messageList", data);
		return new ModelAndView("list.jsp", model);
	}

	private List<Map<String, Object>> prepareListData() {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		for(int i = 0; i < 10; i ++){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", "id"+i);
			map.put("message", "message1");
			list.add(map);
		}
		return list;
	}
}
